package com.probiz.estore.common.model.order;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.Constants;
import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.order.base.SalesOrderTbl;
import com.probiz.estore.common.model.system.SelfCollectionCentre;
import com.probiz.estore.common.util.NumberUtil;
import com.probiz.estore.core.util.ContextUtil;
import com.probiz.estore.order.OrderConstants;
import com.probiz.estore.other.service.TagManager;
import com.probiz.estore.system.service.SelfCollectionCentreManager;

/**
 * Model class for SalesOrder. Add not database mapped fileds in this class.
 */
public class SalesOrder extends SalesOrderTbl {
	
	public static enum FilterHqlName{DEFAULT,ORDER_TOTAL};
	
	
	
  	/**
	 * Default Empty Constructor for class SalesOrder
	 */
	public SalesOrder () {
		super();
	}
	
	/**
	 * 定义实体的业务名取值； salesOrderName
	 * 必须手工完成这个部分，否则编译不通过。
	 */
	public String getSalesOrderName () {
		if (salesOrderId == null)
	        return "";
	    else
			//返回一个指定有业务意义的属性值;
			//如：product的VO就用product.productName
	        return this.orderNo;
	}
	
	/**
	 * Default Key Fields Constructor for class SalesOrder
	 */
	public SalesOrder (
		 Integer in_salesOrderId
		) {
		super (
		  in_salesOrderId
		);
	}
	/**
	 * 获得商品类的销售明细项
	 * @return
	 */
	public List<OrderSku> getOrderSkus4Product(){
		List<OrderSku> orderSkuList = new ArrayList<OrderSku>();
		for(OrderSku orderSku: (Set<OrderSku>)orderSkus){
			if(OrderSku.ITEM_TYPE_PRODUCT.equals(orderSku.getItemType())){
				orderSkuList.add(orderSku);
			}
		}
		return orderSkuList;
	}
	
	/**
	 * 获得礼券类的销售明细项
	 * @return
	 */
	public List<OrderSku> getOrderSkus4Gc(){
		List<OrderSku> orderSkuList = new ArrayList<OrderSku>();
		for(OrderSku orderSku: (Set<OrderSku>)orderSkus){
			if(OrderSku.ITEM_TYPE_GC.equals(orderSku.getItemType())){
				orderSkuList.add(orderSku);
			}
		}
		return orderSkuList;
	}
	
	/**
	 * 获得礼品类的销售明细项
	 * @return
	 */
	public List<OrderSku> getOrderSkus4Gift(){
		List<OrderSku> orderSkuList = new ArrayList<OrderSku>();
		for(OrderSku orderSku: (Set<OrderSku>)orderSkus){
			if(OrderSku.ITEM_TYPE_GIFT.equals(orderSku.getItemType())){
				orderSkuList.add(orderSku);
			}
		}
		return orderSkuList;
	}
	/**
	 * 获得收款单
	 * @return
	 */
	public List<OrderPayment> getOrderPayments4Pay(){
		List<OrderPayment> orderPaymentList = new ArrayList<OrderPayment>();
		for(OrderPayment orderPayment: (Set<OrderPayment>)orderPayments){
			if(OrderPayment.DOCUMENT_TYPE_PAY.equals(orderPayment.getDocumentType())){
				orderPaymentList.add(orderPayment);
			}
		}
		return orderPaymentList;
	}
	/**
	 * 获得已使用的礼券支付项目
	 * @return
	 */
	public List<OrderPayment> getGiftCertificateOrderPayments4Pay(){
		List<OrderPayment> orderPaymentList = new ArrayList<OrderPayment>();
		for(OrderPayment orderPayment: (Set<OrderPayment>)orderPayments){
			if(OrderPayment.DOCUMENT_TYPE_PAY.equals(orderPayment.getDocumentType()) &&
					orderPayment.getPaymentMethodId().equals(OrderConstants.PAYMENT_METHOD_GC) && 
					StringUtils.isNotBlank(orderPayment.getGiftCertificateNo())){
				orderPaymentList.add(orderPayment);
			}
		}
		return orderPaymentList;
	}
	/**
	 * 获得退款单
	 * @return
	 */
	public List<OrderPayment> getOrderPayments4Refund(){
		List<OrderPayment> orderPaymentList = new ArrayList<OrderPayment>();
		for(OrderPayment orderPayment: (Set<OrderPayment>)orderPayments){
			if(OrderPayment.DOCUMENT_TYPE_REFUND.equals(orderPayment.getDocumentType())){
				orderPaymentList.add(orderPayment);
			}
		}
		return orderPaymentList;
	}
	/**
	 * 获得发货单
	 * @return
	 */
	public List<OrderShipment> getOrderShipments4Ship(){
		List<OrderShipment> orderShipmentList = new ArrayList<OrderShipment>();
		for(OrderShipment orderShipment: (Set<OrderShipment>)orderShipments){
			if(OrderShipment.DOCUMENT_TYPE_SHIP.equals(orderShipment.getDocumentType())){
				orderShipmentList.add(orderShipment);
			}
		}
		return orderShipmentList;
	}
	
	
	/**
	 * 获得退货单
	 * @return
	 */
	public List<OrderShipment> getOrderShipments4Return(){
		List<OrderShipment> orderShipmentList = new ArrayList<OrderShipment>();
		for(OrderShipment orderShipment: (Set<OrderShipment>)orderShipments){
			if(OrderShipment.DOCUMENT_TYPE_RETURN.equals(orderShipment.getDocumentType())){
				orderShipmentList.add(orderShipment);
			}
		}
		return orderShipmentList;
	}
	
	/**
	 * 获得购买人名称
	 * @return
	 */
	public String getCustomerFullName(){
		if(StringUtils.isNotBlank(this.getCustomerFirstname())){
			return this.getCustomerFirstname();
		}else{
			return this.getCustomerUsername();
		}
	}
	
	
	/** cash on delivery 货到付款*/
	public boolean isCod(){
		if(this.getIsCod()==null || this.getIsCod().shortValue()==Constants.FLAG_FALSE.shortValue()) 
			return false;
		return true;
	}

	/**
	 * 是否在线支付且未支付
	 * @return
	 */
	public boolean isOnlineUnAllPaid(){
		return OrderConstants.PAYMENT_METHOD_ONLINE.equals(this.getPaymentMethodId())
		&& !OrderConstants.PAYMENT_STATUS_ALL_PAID.equals(this.getPaymentStatus())
		&& (OrderConstants.ORDER_STATUS_AWAITING_CONFIRMATION.equals(this.getOrderStatus()) 
				||  OrderConstants.ORDER_STATUS_CONFIRMED.equals(this.getOrderStatus()) 
				|| OrderConstants.ORDER_STATUS_HANDLING.equals(this.getOrderStatus()));
		
	}
	/**
	 * 客户是否可以取消
	 * @return
	 */
	public boolean isCancelByCustomerEnabled(){
		return ConfigUtil.getInstance().getIsCancelOrderEnabled() 
				&& (OrderConstants.ORDER_STATUS_AWAITING_CONFIRMATION.equals(this.getOrderStatus()) || OrderConstants.ORDER_STATUS_CONFIRMED.equals(this.getOrderStatus()))
				&& NumberUtil.isZero(getMoneyPaid());
	}
		
	
	/**
	 * 获取订单当前要支付的数额
	 * @return
	 */
	public BigDecimal getUnPaidAmount(){
		return this.totalAmount.subtract(this.paidAmount);
	}
	
	
	/**
     * 获得标签名称
     * @return
     */
    public String getTagNames(){
    	TagManager tagManager = (TagManager) ContextUtil.getSpringBeanById("tagManager");
    	return tagManager.getTagNames(this);
    }
    /**
     * 获得标签数组
     * @return
     */
    public String[] getTagNameArray(){
    	TagManager tagManager = (TagManager) ContextUtil.getSpringBeanById("tagManager");
    	return tagManager.getTagNameArray(this);
    }
	
    
    /**
     * 是否上门自提
     */
    public boolean isSelfCollect() {
    	if (this.paymentMethodId != null && this.paymentMethodId.equals(OrderConstants.PAYMENT_METHOD_SELFCOLLECT)) {
    		return true;
    	} else {
    		return false;
    	}
    }
    
    /**
     * 是否需要运输配送
     * @return
     */
	public boolean isShippingRequired() {
		return (this.getOrderAddress() != null) && (!this.getOrderAddress().getIsVirtual());
	}
	
	/**
	 * 计算订单总额
	 */ 
	public BigDecimal calcTotal() {
		BigDecimal result = BigDecimal.ZERO;
		if(null != getProductCost()){
			result = result.add(getProductCost());
		}
		if(null != getWrapCost()){
			result = result.add(getWrapCost());
		}
		if(null != getShippingCost()){
			result = result.add(getShippingCost());
		}
		if(null != getTax()){
			result = result.add(getTax());
		}
		if(null != getDiscount()){
			result = result.subtract(getDiscount());
		}
		if(null != getPremium()){
			result = result.add(getPremium());
		}
		
		return result;
	}
	
	
	public SelfCollectionCentre getSelfCollectionCentre(){
		SelfCollectionCentreManager selfCollectionCentreManager = (SelfCollectionCentreManager)ContextUtil.getInstance().getSpringBeanById("selfCollectionCentreManager");
		if(this.selfCollectionCentreId != null){
			SelfCollectionCentre selfCollectionCentre = selfCollectionCentreManager.getById(this.selfCollectionCentreId);
			try{
				selfCollectionCentre.getSelfCollectionCentreName();
			}catch(Exception e){
				return null;
			}
			return selfCollectionCentre;
		}
		return null;
		
	}

	/**
	 * 金钱已经支付的金额
	 * @return
	 */
	public BigDecimal getMoneyPaid(){
		return paidAmount.subtract(pointPaid).subtract(giftCertificatePaid);
	}
	/**
	 * 金钱未支付的金额 = 未支付金额
	 * @return
	 */
	public BigDecimal getMoneyUnPaid(){
		return this.getUnPaidAmount();
	}
	/**
	 * 是否能退金钱款
	 * 1=能 0=不能
	 * @return
	 */
	public Short getIsCanReturnMoney(){
		if(getMoneyPaid().compareTo(BigDecimal.ZERO) > 0){
			return Constants.FLAG_TRUE;
		}else{
			return Constants.FLAG_FALSE;
		}
			
	}
	
	
	
}
